package com.lanou.projecta_163news.fragment.mainAdapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import com.lanou.projecta_163news.R;

import java.net.URL;
import java.util.List;

/**
 * Created by dllo on 16/3/10.
 */
public class VideoRecyclerAdapter extends RecyclerView.Adapter<VideoRecyclerAdapter.VideoRecyclerViewHolder> {
    private List<Video> videoData;
    private int position;

    public void addvideoData(List<Video> videoData) {
        this.videoData = videoData;
        notifyDataSetChanged();
    }

    @Override
    public VideoRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_video, null);
        return new VideoRecyclerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(VideoRecyclerViewHolder holder, int position) {
        if (videoData != null && videoData.size() > 0) {
            Video video = videoData.get(position);
            holder.videoTitle.setText(video.getVideoTitle());
            holder.videoContent.setText(video.getVideoContent());
            holder.video.setVideoURI(Uri.parse(video.getVideoUrl()));
            holder.video.requestFocus();
            holder.position = position;
        }

    }

    @Override
    public int getItemCount() {
        return videoData != null && videoData.size() > 0 ? videoData.size() : 0;
    }

    public class VideoRecyclerViewHolder extends RecyclerView.ViewHolder {
        private VideoView video;
        private TextView videoTitle;
        private TextView videoContent;
        private int position;

        public VideoRecyclerViewHolder(View itemView) {
            super(itemView);
            video = (VideoView) itemView.findViewById(R.id.videoview);
            videoTitle = (TextView) itemView.findViewById(R.id.video_item_title);
            videoContent = (TextView) itemView.findViewById(R.id.video_item_content);
            video.setMediaController(new MediaController(itemView.getContext()));
            Uri uri = Uri.parse("http://222.163.239.229/flv2.bn.netease.com/videolib3/1603/09/OoUpN9394/SD/OoUpN9394-mobile.mp4?wsiphost=local");
            video.setVideoURI(uri);
            video.requestFocus();

        }
    }
}
